//
//  BookingEditTransitionAnimator.m
//  OfficeBook
//
//  Created by 孙晓奇 on 15/12/15.
//  Copyright © 2015年 sunny. All rights reserved.
//

#import "BookingEditTransitionAnimator.h"


@implementation BookingEditTransitionAnimator

-(instancetype)init{
    self = [super init];
    if (self) {
        self.vcHeight = 120;
    }
    return self;
}
#pragma mark - UIViewControllerAnimatedTransitioning

- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
    UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    UIView *fromView = fromVC.view;
    UIView *toView = toVC.view;
    UIView *containerView = [transitionContext containerView];
    NSTimeInterval duration = [self transitionDuration:transitionContext];
    
    // 从上往下
    CGRect fromFrame    = [transitionContext initialFrameForViewController:fromVC];
    CGRect initialFrame = CGRectMake(0, - (CGRectGetHeight(fromFrame) - self.vcHeight),
                                     CGRectGetWidth(fromFrame), CGRectGetHeight(fromFrame) - self.vcHeight);
    CGRect finalFrame   = CGRectMake(0, 0, CGRectGetWidth(initialFrame), CGRectGetHeight(initialFrame));
    
    // Presenting
    if (self.appearing) {
        fromView.userInteractionEnabled = NO;
        
        toView.frame = initialFrame;
        [containerView addSubview:toView];
        
        [UIView animateWithDuration:duration animations: ^{
            toView.frame = finalFrame;
            fromView.alpha = 0.6;
        } completion: ^(BOOL finished) {
            [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
        }];
    }
    // Dismissing
    else {
        [UIView animateWithDuration:duration animations: ^{
            fromView.frame = initialFrame;
            toView.alpha = 1.0;
        } completion: ^(BOOL finished) {
            [fromView removeFromSuperview];
            toView.userInteractionEnabled = YES;
            [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
        }];
    }
}

@end
